package com.ijiuchuang.jc_localization.presenter.settings;

import android.content.Context;

import com.ijiuchuang.jc_localization.bean.bmob.AppVersion;
import com.ijiuchuang.jc_localization.bean.lite.Setts;
import com.ijiuchuang.jc_localization.contract.settings.SettingsContract;
import com.ijiuchuang.jc_localization.model.settings.SettingsModel;
import com.ijiuchuang.jc_localization.tools.utils.Log;
import com.ijiuchuang.jc_localization.tools.utils.MyUtil;
import com.ijiuchuang.jc_localization.tools.utils.Toastor;

import rx.Subscriber;

/**
 * Created by Veev on 2016/6/17
 *
 * @link 384506557@qq.com
 */
public class SettingsPresenter implements SettingsContract.Presenter {

    private Context mContext;
    private SettingsContract.View view;
    private SettingsContract.Model model;

    public SettingsPresenter(Context mContext, SettingsContract.View view) {
        this.mContext = mContext;
        this.view = view;
        model = new SettingsModel();
    }

    @Override
    public void start() {
        // start here
        loadConfig();
    }

    @Override
    public void checkVersion() {
        model.checkVersion(new Subscriber<AppVersion>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(AppVersion appVersion) {
                if (appVersion.getVersion_i() != MyUtil.getVersionCode(mContext)) {
                    Log.i("发现新版本， 是否更新");
                    Log.i("版本更新日志: " + appVersion.getUpdate_log());
                    Toastor.with(mContext).setText("发现新版本: " + appVersion.getVersion()).show();
                } else {
                    Toastor.with(mContext).setText("已经是最新版本").show();
                }
            }
        });
    }

    @Override
    public void save(String key, int value) {
        model.save(key, value);
    }

    @Override
    public void loadConfig() {
        view.setBannerMode(model.loadInteger(Setts.SETTINGS_HOME_BANNER_MODE));
    }
}